<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>牧心农业</title>
    <link rel="stylesheet" type="text/css" href="/static/css/eui.css" />
    <link rel="stylesheet" type="text/css" href="/static/css/mobile.css" />
    <link rel="stylesheet" type="text/css" href="/static/css/iconfont.css" />
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=GKTGV62UVGc1FZb4wUBdWG8w"></script>
    <script src="/static/js/jquery.min.js"></script>
    <script src="/static/js/doT.min.js"></script>
    <script src="/static/js/func.js"></script>
</head>
<body>
    <div class="content margin_top">
        <header class="header fixed_top bg_mx_green"></header>
        <form class="inspection"></form>
    </div>
    <div class="toast"></div>
    <!-- toast模版 -->
    <script type="text/template" charset="utf-8" id='toast_template'>
        <i class="iconfont icon-toast{{=it.icon}}"></i>
        <div class="toast_text">{{=it.text}}</div>
    </script>
    <script type="text/template" charset="utf-8" id='info_template'>
        <div class="bg_white margin_b_10">
            <div class="flex border_b padding_10_15 border_b">
                <div class="width_70">健康状况</div>
                <div class="left_70 light">
                    <select name="healthy">
                        <{foreach from=$healthy_status key=k item=value }>
                            <option value="<{$value}>"><{$value}></option>
                        <{/foreach}>
                    </select>
                </div>
            </div>
            <div class="text_info padding_10_15">
                <textarea name="healthy_desc" placeholder="请输入健康描述" request title="健康描述"></textarea>
            </div>
        </div>
        <div class="bg_white margin_b_10">
            <div class="flex border_b padding_10_15 border_b">
                <div class="width_70">体测状况</div>
                <div class="left_70 light">
                    <select name="situation" id="situation">
                        <{foreach from=$body_status key=k item=value }>
                            <option value="<{$value}>"><{$value}></option>
                        <{/foreach}>
                    </select>
                </div>
            </div>
            <div class="padding_h_15 padding_v_5 border_b row lineheight_3">
                {{for(var i in it){}}
                <div class="col_6 s_text flex">
                    <div class="width_70">{{=it[i].name}}</div>
                    {{if(it[i].type=='input'){}}
                        <div class="left_70 light">
                            <input type="text" id="{{=it[i].field}}" name="{{=it[i].field}}" placeholder="{{=it[i].unit||'请输入'}}" request title="{{=it[i].name}}" {{if(it[i].ltype=='int'){}} data-pattern="^[0-9]+(.[0-9]{1,2})?$" data-tips="最多两位小数的数字"{{}else if(it[i].ltype=='char'){}} data-pattern="^[\u4e00-\u9fa5_a-zA-Z]+$" data-tips="非数字的字符"{{}}}>
                        </div>
                    {{}else if(it[i].type=='select'){}}
                        <div class="left_70 light">
                            <select name="{{=it[i].field}}" id="{{=it[i].field}}">
                                {{for(var j in it[i].data){}}
                                <option value="{{=it[i].data[j]}}">{{=it[i].data[j]}}</option>
                                {{}}}
                            </select>
                        </div>
                    {{}}}
                </div>
                {{}}}
            </div>
            <div class="padding_15">
                <div class="preview0 flex_row_3 margin_b_10"></div>
                <div class="light lineheight_3 v_center input0">
                    <div class="width_50 border text_center relative">
                        <div class="iconfont icon-add font_20"></div>
                        <input class="absolute all" type="file" value="" accept="image/*" style="opacity:0;" onchange="selectpic(this,0)"/>
                        <input type="hidden" name="pigimg" request title="仔猪照片"  value=""/>
                    </div>
                    <div class="left_50  padding_l_15">点击上传图片</div>
                </div>
            </div>
        </div>
        <div class="bg_white margin_b_10">
            <div class="flex border_b padding_10_15 border_b">
                <div class="width_70">食料情况</div>
                <div class="left_70 light">
                    <select name="foodssituation">
                        <{foreach from=$pigpen_status key=k item=value }>
                            <option value="<{$value}>"><{$value}></option>
                        <{/foreach}>
                    </select>
                </div>
            </div>
            <div class="text_info padding_10_15 border_b">
                <textarea name="foods_desc" placeholder="请输入猪所吃的食料" request title="食料情况"></textarea>
            </div>
            <div class="padding_15">
                <div class="preview1 flex_row_3 margin_b_10"></div>
                <div class="light lineheight_3 v_center input1">
                    <div class="width_50 border text_center">
                        <div class="iconfont icon-add font_20"></div>
                        <input class="absolute all" type="file" value="" accept="image/*" style="opacity:0;" onchange="selectpic(this,1)"/>
                        <input type="hidden" name="foodsimg" value="" request title="食料照片"/>
                    </div>
                    <div class="left_50  padding_l_15">点击上传图片</div>
                </div>
            </div>
        </div>
        <div class="bg_white margin_b_10">
            <div class="flex border_b padding_10_15 border_b">
                <div class="width_70">猪舍环境</div>
                <div class="left_70 light">
                    <select name="pigpen">
                        <{foreach from=$pigpen_status key=k item=value }>
                            <option value="<{$value}>"><{$value}></option>
                        <{/foreach}>
                    </select>
                </div>
            </div>
            <div class="text_info padding_10_15 border_b">
                <textarea name="pigpen_desc" placeholder="请输入猪舍详细描述" request title="猪舍描述"></textarea>
            </div>
            <div class="padding_15">
                <div class="preview2 flex_row_3 margin_b_10"></div>
                <div class="light lineheight_3 v_center input2">
                    <div class="width_50 border text_center">
                        <div class="iconfont icon-add font_20"></div>
                        <input class="absolute all" type="file" value="" accept="image/*" style="opacity:0;" onchange="selectpic(this,2)"/>
                        <input type="hidden" name="penimg" value="" request title="猪舍照片"/>
                    </div>
                    <div class="left_50  padding_l_15">点击上传图片</div>
                </div>
            </div>
        </div>
        <div class="padding_15 font_16">
            <button class="btn btn_mx_green all_width lineheight_3">保存</button>
        </div>
        <input name="_csrf" type="hidden" id="_csrf" value="<{$app->request->csrfToken}>">
        <input type="hidden" name="pigid">
        <input type="hidden" name="province">
        <input type="hidden" name="city">
        <input type="hidden" name="district">
        <input type="hidden" name="street">
        <input type="hidden" name="lng">
        <input type="hidden" name="lat">
    </script>
    <script type="text/template" charset="utf-8" id='img_template'></script>
    <script>
        // 页面初始化
            var n = <{$items}>;console.log(n)
            var evalinfo = doT.template($("#info_template").text());
            var evalToast = doT.template($("#toast_template").text());
            loaddata(".inspection",evalinfo,n);
            loadheadbar("常规巡检",1)
        // 选择图片
            var img_arr=[[],[],[]],imgs=[[],[],[]],evalImg;
            loadimg(function(){
                evalImg = doT.template($("#img_template").text())
            });
            function selectpic(obj,i){
                var file = obj.files[0];
                var URL = window.URL || window.webkitURL;
                tobase64(file,function(res){
                    imgs[i].push(res);
                })
                img_arr[i].push(URL.createObjectURL(file))
                $(".preview"+i).html(evalImg({n:i,d:img_arr[i]}));console.log(img_arr[i])
                $(obj).val("");
                if(img_arr[i].length>=3){
                    $(".input"+i).hide();
                }
            }
        // 删除图片
            function del(n,i){
                imgs[n].splice(i,1);
                img_arr[n].splice(i,1);
                $(".preview"+n).html(evalImg({n:n,d:img_arr[n]}));
                $(".input"+n).show();
            }
        // 定位
            var geolocation = new BMap.Geolocation();
            geolocation.getCurrentPosition(function(r){
                if(this.getStatus() == BMAP_STATUS_SUCCESS){
                    $("input[name=lng]").val(r.point.lng)
                    $("input[name=lat]").val(r.point.lat)
                    $("input[name=province]").val(r.address.province)
                    $("input[name=city]").val(r.address.city)
                    $("input[name=district]").val(r.address.district)
                    $("input[name=street]").val(r.address.street)
                }else {
                    alert('failed');
                }
            },{enableHighAccuracy: true})
        // 提交表单
            var submited = false;
            $(".inspection").submit(function(e){
                e.preventDefault();
                var pigid = GetQueryString("pigid");
                $("input[name=pigid]").val(pigid)
                with(this){
                    pigimg.value = imgs[0].join("%^");
                    foodsimg.value = imgs[1].join("%^");
                    penimg.value = imgs[2].join("%^");
                    for (var i = 0; i < this.length; i++) {
                        if($(this[i]).attr("request")==""){
                            if(!checkone(this[i])){
                                return false;
                            }
                        }
                    }
                    if(submited) return false;
                    var data = $(".inspection").serialize();
                    $.post("/business/person/post-inspection",data,function(res){console.log(res)
                        submited = true;
                        toast("attention",res.msg);
                        if(res.status == 0){
                            location.href='/business/person/pig';
                        }
                    },"json")
                }
            });



        //根据公式计算体重
        $('#xishu').change(function(){
                var weight = parseInt($('#width').val())*parseInt($('#length').val())/$(this).val();
                if(isNaN(weight)){
                   if($('#width').val() == ''){
                       toast("wrong","胸围不能为空");
                       $('#width').focus();
                       return false
                   }
                   if($('#length').val() == ''){
                        toast("wrong","体长不能为空");
                        $('#length').focus();
                        return false;
                   }
                }
                $('#weight').val(weight.toFixed(2));
        })
        



    </script>
</body>
</html>
